package com.my.study.juc.basic.syn;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/4/8 23:57
 **/
public class UnsafeBank {

    public static void main(String[] args) {
        Account account = new Account("基金", 100);

        Drawing you = new Drawing(account, 50, "你");
        Drawing wife = new Drawing(account, 100, "wife");

        you.start();
        wife.start();

    }

}

/**
 * 账户
 */
class Account {
    private String name;
    private int money;

    public Account(String name, int money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

/**
 * 取钱
 */
class Drawing extends Thread{

    /**
     * 账户
     */
    Account account;
    /**
     * 本次取钱数量
     */
    int drawing;
    /**
     * 剩余的钱
     */
    int nowMoney;

    public Drawing(Account account, int drawing, String name) {
        super(name);
        this.account = account;
        this.drawing = drawing;
    }

    /**
     * 取钱
     */
    @Override
    public void run() {
        if (account.getMoney() - drawing < 0) {
            System.out.println(this.getName() + "钱不够，取不了。");
            return;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int money = account.getMoney() - drawing;
        account.setMoney(money);

        nowMoney = nowMoney + drawing;

        System.out.println(account.getName() + " 余额为：" + account.getMoney());
        System.out.println(this.getName() + "手里的钱：" + nowMoney);
    }
}
